﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

using agvcontroller.Common;
using agvcontroller.Control;

namespace agvcontroller.GUI
{
    /// <summary>
    /// Interaction logic for UCSocketMsgViewerk.xaml
    /// </summary>
    public partial class UCSocketMsgViewer : UserControl
    {
        CVehicle Vehicle;
        Timer tmrStatus =null;

        public UCSocketMsgViewer(CVehicle vehicle)
        {
            InitializeComponent();
        
            tmrStatus = new Timer(new TimerCallback(this.Tick), null, 0, 1000);

            Vehicle.onRcvMsg += new deleRcvMsg(Vehicle_onRcvMsg);
            Vehicle.onReportConnection +=new deleReportConnection(Vehicle_onReportConnection);
        }

        void Vehicle_onRcvMsg(object sender, string msg)
        {
            DisplayMsg(msg);

        }

        void Vehicle_onReportConnection(object sender, enumConnect connect)
        {
            if (connect == enumConnect.CONNECTION)
                DisplayMsg("Connected~");

            else
                DisplayMsg("Disconnected~");
        }

        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            Vehicle.SendMsg(tbSend.Text);
        }

        public void Tick(object state)
        {
            
        }

        void DisplayMsg(string msg)
        {
            System.Windows.Threading.DispatcherOperation
         dispatcherOp = tbMsg.Dispatcher.BeginInvoke(
         System.Windows.Threading.DispatcherPriority.Normal,
         new Action(
           delegate()
           {
               tbMsg.Text += msg;
               tbMsg.Text += "\r\n";
           }
       ));

            
        }
    }
}
